from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class ShoppingCartPage:
    def __init__(self, driver):
        self.driver = driver
        # 页面元素定位器
        self.page_title_locator = (By.XPATH, "//h1[@class='page-title']")
        self.product_item_locator = (By.ID, "product-item")
        self.product_name_locator = (By.XPATH, "//div[@class='product-name']/a")
        self.product_quantity_input_locator = (By.ID, "quantity")
        self.product_price_locator = (By.XPATH, "//span[@class='price']")
        self.update_cart_button_locator = (By.ID, "update-cart")
        self.remove_product_button_locator = (By.XPATH, "//button[text()='Remove']")
        self.empty_cart_message_locator = (By.ID, "empty-cart-message")
        self.proceed_to_checkout_button_locator = (By.ID, "checkout")
        self.subtotal_locator = (By.ID, "subtotal")
        self.total_locator = (By.ID, "total")
        self.promo_code_input_locator = (By.ID, "promo-code")
        self.apply_promo_code_button_locator = (By.ID, "apply-promo-code")
        self.discount_locator = (By.ID, "discount")
        self.clear_cart_button_locator = (By.ID, "clear-cart")

    def get_page_title(self):
        page_title = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.page_title_locator)
        )
        return page_title.text

    def get_product_name(self):
        product_name = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.product_name_locator)
        )
        return product_name.text

    def set_product_quantity(self, quantity):
        quantity_input = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.product_quantity_input_locator)
        )
        quantity_input.clear()
        quantity_input.send_keys(quantity)

    def click_update_cart_button(self):
        update_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.update_cart_button_locator)
        )
        update_button.click()

    def click_remove_product_button(self):
        remove_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.remove_product_button_locator)
        )
        remove_button.click()

    def get_empty_cart_message(self):
        empty_message = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.empty_cart_message_locator)
        )
        return empty_message.text

    def click_proceed_to_checkout_button(self):
        checkout_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.proceed_to_checkout_button_locator)
        )
        checkout_button.click()

    def get_subtotal(self):
        subtotal = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.subtotal_locator)
        )
        return subtotal.text

    def get_total(self):
        total = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.total_locator)
        )
        return total.text

    def enter_promo_code(self, promo_code):
        promo_code_input = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.promo_code_input_locator)
        )
        promo_code_input.send_keys(promo_code)

    def click_apply_promo_code_button(self):
        apply_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.apply_promo_code_button_locator)
        )
        apply_button.click()

    def get_discount(self):
        discount = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.discount_locator)
        )
        return discount.text

    def click_clear_cart_button(self):
        clear_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.clear_cart_button_locator)
        )
        clear_button.click()